import { Event } from 'three';
import ThreeApp from '../ThreeApp';

export default class Controller {
  public isEnable: boolean = false;
  public readonly events: Map<string, any> = new Map();

  public addEvent(type: string, callback: (event: Event) => void) {
    const fun = callback.bind(this);
    ThreeApp.addEventListener(type, fun);
    this.events.set(type, fun);
  }

  public removeEvent(type: string) {
    const value = this.events.get(type);
    if (value) {
      ThreeApp.removeEventListener(type, value);
    }
  }

  public destory() {
    this.events.forEach((value, key) => {
      ThreeApp.removeEventListener(key, value);
    });
  }
}
